package com.jtjrenren.android.taxi.driver.core.user.register;

import android.graphics.Bitmap;

import com.jtjrenren.android.taxi.driver.bean.api.ApiCompanyList;
import com.jtjrenren.android.taxi.driver.bean.event.CompanyListEvent;
import com.jtjrenren.android.taxi.driver.bean.event.PhoneUniEvent;
import com.jtjrenren.android.taxi.driver.bean.event.RegisterEvent;
import com.jtjrenren.android.taxi.driver.bean.event.UpPicEvent;
import com.jtjrenren.android.taxi.driver.bean.event.YzmCheckEvent;
import com.jtjrenren.android.taxi.driver.utils.ModuleUtils;
import com.library.mvp.presenter.MvpBasePresenter;
import com.library.utils.LogUtil;
import com.library.utils.RegexUtils;
import com.library.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;


/**
 * Created by Administrator on 2016/8/19.
 */

public class RegisterPresenterImpl extends MvpBasePresenter<RegisterView, RegisterInterctor> implements RegisterPresenter {

    private String phone, pwd, name, yybh, carcode, sfzh, icon_name, zs_name;
    private ApiCompanyList.CompanyListEntity company;
    private ApiCompanyList apiCompanyList;
    private int curr_step = 0;

    public RegisterPresenterImpl(RegisterView view) {
        super(view);
    }

    @Subscribe
    public void handRegisterEvent(RegisterEvent event) {
        view.hideLoadding();
        String desc = event.getDesc();
        view.showTip(desc);
        if (event.isSuccess()) {
            view.registerSucess();
        }
    }

    @Subscribe
    public void handCheckYzmEvent(YzmCheckEvent event) {
        view.hideLoadding();
        if (event.isPass()) {
            view.showStep2();
            interactor.getCompanyList();
        } else
            view.showTip(event.getDesc());
    }

    @Subscribe
    public void handUpPicEvent(UpPicEvent event) {
        LogUtil.log("register", "Thread:" + Thread.currentThread().toString());
        view.hideLoadding();
        if (event.isSuccess()) {
            if (curr_step == 2) {
                icon_name = event.getDesc();
                view.showStep3();
            } else if (curr_step == 3) {
                zs_name = event.getDesc();
                register();
            }
        } else
            view.showTip(event.getDesc());
    }

    @Subscribe
    public void handPhoneUniEvent(PhoneUniEvent event) {
        view.hideLoadding();
        if (event.isUni())
            view.showTip(event.getDesc());
        else {
            view.getYzmStart();
            interactor.getYzm(phone, "0");
        }
    }

    @Subscribe
    public void handCompanyListEvent(CompanyListEvent event) {
        apiCompanyList = event.getApiCompanyList();
        if (apiCompanyList != null && apiCompanyList.getCompanyList().size() > 0) {
            List<ApiCompanyList.CompanyListEntity> companyList = apiCompanyList.getCompanyList();
            String[] companys = new String[companyList.size()];
            for (int i = 0; i < companyList.size(); i++) {
                companys[i] = companyList.get(i).getCOMPANYNAME();
            }
            view.refreshCompanyList(companys);
        }
    }

    @Override
    public RegisterInterctor createInteractor() {
        return new RegisterInterctorImpl();
    }

    @Override
    public void init() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void destory() {
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void getYzm(String phone) {
        //获取验证码之前，首先对手机的正确性进行检查，第二步判断该手机号是否已经注册了，第三步调用api发送验证码
        String desc = ModuleUtils.checkPhone(phone);
        if (desc != null) {
            view.showTip(desc);
            return;
        }
        this.phone = phone;
        view.showLoadding("验证手机号中，请稍候....");
        interactor.checkPhoneUni(phone);
    }

    @Override
    public void upPic(Bitmap bitmap) {
        if (bitmap == null)
            return;
        view.showLoadding("图像上传中，请稍候..");
        interactor.upPic(bitmap);
    }

    @Override
    public void doStep1(String yzm, String pwd, boolean xyChecked) {

        if (!xyChecked) {
            view.showTip("阅读并同意 使用协议才能注册");
            return;
        }

        if (pwd.length() < 6) {
            view.showTip("请输入长度大于6位的密码");
            return;
        }

        if (StringUtils.isEmpty(yzm)) {
            view.showTip("请输入正确的验证码");
            return;
        }

        this.pwd = pwd;
        view.showLoadding("正在校验验证码,请稍候..");
        interactor.checkYzm(phone, yzm, "0");
    }

    @Override
    public void doStep2(String name, String yybh, String carcode, String sfzh, int comPos, Bitmap icon) {

        if (StringUtils.isEmpty(name) && !RegexUtils.match("[\u4e00-\u9fa5]{2,6}", name)) {
            view.showTip("请输入正确的名字");
            return;
        }
        if (StringUtils.isEmpty(yybh) && !RegexUtils.match("[a-zA-Z0-9]+", yybh)) {
            view.showTip("请输入正确的运营证编号");
            return;
        }
        if (carcode.length() < 7 && RegexUtils.match("^[\u4e00-\u9fa5]{1}[A-Za-z0-9]{6}$", carcode)) {
            view.showTip("请输入正确的车牌号码");
            return;
        }
        if (sfzh.length() < 18 && !RegexUtils.match("^[\\d]{17}[x|X|\\d]{1}$", sfzh)) {
            view.showTip("请输入正确的身份证号码");
            return;
        }
        if (icon == null) {
            view.showTip("请设置头像");
            return;
        }

        this.name = name;
        this.yybh = yybh;
        this.carcode = carcode;
        this.sfzh = sfzh;
        if (apiCompanyList != null)
            this.company = apiCompanyList.getCompanyList().get(comPos);
        else
            this.company = null;

        curr_step = 2;
        upPic(icon);
    }

    @Override
    public void doStep3(Bitmap bitmap) {
        if (bitmap == null) {
            view.showTip("请上传运营资格证");
            return;
        }

        curr_step = 3;
        upPic(bitmap);
    }

    private void register() {
        view.showLoadding("注册中，请稍候...");

        interactor.register(phone, ModuleUtils.DesEncry(pwd), company.getCOMPANYID(), name, yybh, carcode, sfzh, icon_name, zs_name);
    }
}
